package com.css.fxfzdzzh.modules.basicData.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzdzzh.base.response.RestResponse;
import com.css.fxfzdzzh.constants.FxfzConstants;
import com.css.fxfzdzzh.enums.DataCategoryEnum;
import com.css.fxfzdzzh.modules.basicData.param.sysDataVersion.DataQueryParam;
import com.css.fxfzdzzh.modules.basicData.param.sysDataVersion.SysDataVersionPageParam;
import com.css.fxfzdzzh.modules.drill.service.DrillService;
import com.css.fxfzdzzh.modules.eqLandslide.count.entity.JcPga002;
import com.css.fxfzdzzh.util.CreateGson;
import com.css.fxfzdzzh.util.PlatformObjectUtils;
import com.css.fxfzdzzh.util.ResponseCreator;
import com.css.fxfzdzzh.web.PlatformSessionContext;
import com.css.fxfzdzzh.zcpt.sys.entity.SUser;
import com.css.fxfzdzzh.zcpt.sys.service.SUserService;
import com.css.fxfzdzzh.zcpt.sys.service.SysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/** 数据版本管理 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class BasicLandformService {

    @Resource
    private DrillService drillService;

    @Value("${image.localDir}")
    private String localDir;

    @Value("${basicData.url}")
    public String basicDataUrl;
    @Resource
    SUserService sUserService;
    @Resource
    SysRoleService sysRoleService;

    public static final String DATA_VERSION_CODE_PREFIX = "JC";

    @Operation(summary = "查询数据类别树")
    public RestResponse typeTree(String dictCode) {
        String url = basicDataUrl + "/test/dictItems/tree/"+dictCode;
        return drillService.getJCSJ(url,3);
    }

    @Operation(summary = "根据条件查询数据版本列表接口（无分页）")
    public RestResponse versionList(SysDataVersionPageParam pageParam) {
        Integer hasNationwide = 1;
        String userId = PlatformSessionContext.getUserID();
        String user = sUserService.getSUser(userId);
        if(PlatformObjectUtils.isEmpty(user)){
            return ResponseCreator.createFailResponse("查询不到用户信息");
        }
        String province = "";
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
        String role = sysRoleService.getRoleByUserId(userId);
        List<Map> roleMaps = JSONObject.parseArray(role, Map.class);
        StringBuilder str=new StringBuilder("");
        for(Map roleMap:roleMaps){
            str.append(roleMap.get("roleCode")+",");
        }
        String roles = str.toString();
        if(roles.contains(FxfzConstants.DZZH_COUN_GL_ROLE) || roles.contains(FxfzConstants.DZZH_COUN_YW_ROLE)){
            //国家级管理员和国家级业务人员可以全看国家级数据
        }else if(roles.contains(FxfzConstants.DZZH_PRI_GL_ROLE) || roles.contains(FxfzConstants.DZZH_PRI_YW_ROLE)){
            province = sUser.getProvinceId();
        }

        pageParam.setProvince(province);
        pageParam.setHasNationwide(hasNationwide);
        String dataCategory = pageParam.getDataCategory();
        if(DataCategoryEnum.RIVERSYSTEM.getCategory().equals(dataCategory)){
            //水系
            pageParam.setDataType2("70");
        }else if (DataCategoryEnum.LITHOLOGY.getCategory().equals(dataCategory)){
            //底层岩性
            pageParam.setDataType2("94");
        }else if (DataCategoryEnum.PGA.getCategory().equals(dataCategory)){
            //地震动峰值加速度数据
            if("002".equals(pageParam.getProbability())){
                pageParam.setDataType2("100");
            }else if("010".equals(pageParam.getProbability())){
                pageParam.setDataType2("101");
            }else if("063".equals(pageParam.getProbability())){
                pageParam.setDataType2("102");
            }
        }else if (DataCategoryEnum.CLIMATIC.getCategory().equals(dataCategory)){
            //气候
            pageParam.setDataType2("67");
        }else if (DataCategoryEnum.LANDFORM.getCategory().equals(dataCategory)){
            //地形地貌
            pageParam.setDataType2("68");
        }else if (DataCategoryEnum.ACTIVEFAULT.getCategory().equals(dataCategory)){
            //活断层
            pageParam.setDataType2("93");
        }else if (DataCategoryEnum.LOAD.getCategory().equals(dataCategory)){
            //道路
            pageParam.setDataType2("124");
        }
        String url = basicDataUrl + "/datacenter/sysDataVersion/page?dataType2="+pageParam.getDataType2();
        if(pageParam.getDataRangeCountry()!=null){
            url += "&dataRangeCountry="+pageParam.getDataRangeCountry();
        }
        if(pageParam.getUploadStartTime()!=null){
            url += "&uploadStartTime="+pageParam.getUploadStartTime();
        }
        if(pageParam.getUploadEndTime()!=null){
            url += "&uploadEndTime="+pageParam.getUploadEndTime();
        }
        if(pageParam.getUploadUsername()!=null){
            url += "&uploadUsername="+pageParam.getUploadUsername();
        }
        if(pageParam.getVersionDesc()!=null){
            url += "&versionDesc="+pageParam.getVersionDesc();
        }
        url += "&page="+pageParam.getPage()+"&rows="+pageParam.getRows()+"&hasNationwide="+hasNationwide;
        //String url = basicDataUrl + "/datacenter/sysDataVersion/list?dataType2="+pageParam.getDataType2()+"&dataRangeCountry="+pageParam.getDataRangeCountry()+"&page="+pageParam.getPage()+"&rows="+pageParam.getRows()+"&hasNationwide="+hasNationwide;
        return drillService.getJCSJ(url,2);
    }

    public RestResponse queryDataByVersionCode(String dataCategory, String probability,String versionCode,String geomType,Integer page, Integer rows, HttpHeaders headers) {
        String typeCode = "";
        if(DataCategoryEnum.RIVERSYSTEM.getCategory().equals(dataCategory)){
            //水系
            //versionCode = "JC01JCDL20230202013";
            typeCode = "sxx";
        }else if (DataCategoryEnum.LITHOLOGY.getCategory().equals(dataCategory)){
            //底层岩性
            //versionCode = "JC01DZQH20230208001";
            typeCode = "dcyx1";
        }else if (DataCategoryEnum.PGA.getCategory().equals(dataCategory)){
            //地震动峰值加速度数据
            if("002".equals(probability)){
                //versionCode = "JC01DZQH20230203017";
                typeCode = "pga002";
            }else if("010".equals(probability)){
                //versionCode = "JC01DZQH20230203018";
                typeCode = "pga010";
            }else if("063".equals(probability)){
                //versionCode = "JC01DZQH20230203019";
                typeCode = "pga063";
            }
        }else if (DataCategoryEnum.CLIMATIC.getCategory().equals(dataCategory)){
            //气候
            //versionCode = "JC01JCDL20230210002";
            typeCode = "qh";
        }else if (DataCategoryEnum.LANDFORM.getCategory().equals(dataCategory)){
            //地形地貌
            //versionCode = "JC01JCDL20230210002";
            typeCode = "demgcsj";
        }else if (DataCategoryEnum.ACTIVEFAULT.getCategory().equals(dataCategory)){
            //活断层
            //versionCode = "JC01JCDL20230210002";
            typeCode = "dc";
        }else if (DataCategoryEnum.LOAD.getCategory().equals(dataCategory)){
            //道路
            //versionCode = "JC01JCDL20230210002";
            typeCode = "dl1";
        }
        String url = basicDataUrl + "/datacenter/sysDataVersion?versionCode="+versionCode+"&typeCode="+typeCode
                +"&page="+page+"&rows="+rows+"&geomType="+geomType;
        return drillService.getJCSJ(url, 2);
//        JSONObject json = (JSONObject) JSON.toJSON(jcsj.getData());
//        List<JcPga002> jcPga002s = (List<JcPga002>)json.get("data");
//        return RestResponse.succeed(jcPga002s);
    }

    public RestResponse queryAllByVersionCode(String dataCategory, String probability,String versionCode,String geomType, HttpHeaders headers) {
        String typeCode = "";
        if(DataCategoryEnum.RIVERSYSTEM.getCategory().equals(dataCategory)){
            //水系
            //versionCode = "JC01JCDL20230202013";
            typeCode = "sxx";
        }else if (DataCategoryEnum.LITHOLOGY.getCategory().equals(dataCategory)){
            //底层岩性
            //versionCode = "JC01DZQH20230208001";
            typeCode = "dcyx1";
        }else if (DataCategoryEnum.PGA.getCategory().equals(dataCategory)){
            //地震动峰值加速度数据
            if("002".equals(probability)){
                //versionCode = "JC01DZQH20230203017";
                typeCode = "pga002";
            }else if("010".equals(probability)){
                //versionCode = "JC01DZQH20230203018";
                typeCode = "pga010";
            }else if("063".equals(probability)){
                //versionCode = "JC01DZQH20230203019";
                typeCode = "pga063";
            }
        }else if (DataCategoryEnum.CLIMATIC.getCategory().equals(dataCategory)){
            //气候
            //versionCode = "JC01JCDL20230210002";
            typeCode = "qh";
        }else if (DataCategoryEnum.LANDFORM.getCategory().equals(dataCategory)){
            //地形地貌
            //versionCode = "JC01JCDL20230210002";
            typeCode = "demgcsj";
        }else if (DataCategoryEnum.ACTIVEFAULT.getCategory().equals(dataCategory)){
            //活断层
            //versionCode = "JC01JCDL20230210002";
            typeCode = "dc";
        }else if (DataCategoryEnum.LOAD.getCategory().equals(dataCategory)){
            //道路
            //versionCode = "JC01JCDL20230210002";
            typeCode = "dl1";
        }
        String url = basicDataUrl + "/datacenter/sysDataVersion/queryAllByVersionCode?versionCode="+versionCode+"&typeCode="+typeCode
                +"&geomType="+geomType;
        return drillService.getJCSJ(url, 2);
//        JSONObject json = (JSONObject) JSON.toJSON(jcsj.getData());
//        List<JcPga002> jcPga002s = (List<JcPga002>)json.get("data");
//        return RestResponse.succeed(jcPga002s);
    }

    public RestResponse queryByVersionCode(String typeCode, String versionCode, Integer page, Integer rows, HttpHeaders headers) {
        String url = basicDataUrl + "/datacenter/sysDataVersion/queryDataByVersionCode?versionCode="+versionCode+"&typeCode="+typeCode
                +"&page="+page+"&rows="+rows;
        return drillService.getJCSJ(url, 2);
    }

    public RestResponse queryDataById(String id, String typeCode, HttpHeaders headers) {
        String url = basicDataUrl + "/datacenter/sysDataVersion/queryDataById?id="+id+"&typeCode="+typeCode;
        return drillService.getJCSJ(url, 2);
    }

    public RestResponse list(DataQueryParam pageParam) {
//        String url = basicDataUrl + "/datacenter/sysDataVersion/page?dataType2="+pageParam.getDataType2();//分页
        String url = basicDataUrl + "/datacenter/sysDataVersion/list?dataType2="+pageParam.getDataType2();//不分页
        if(pageParam.getDataRangeProvince()!=null){
            url += "&dataRangeProvince="+pageParam.getDataRangeProvince();
        }
        if(pageParam.getDataRangeCity()!=null){
            url += "&dataRangeCity="+pageParam.getDataRangeCity();
        }
        if(pageParam.getDataRangeArea()!=null){
            url += "&dataRangeArea="+pageParam.getDataRangeArea();
        }
        if(pageParam.getDataRangeCountry()!=null){
            url += "&dataRangeCountry="+pageParam.getDataRangeCountry();
        }
        if(pageParam.getVersionCode()!=null){
            url += "&versionCode="+pageParam.getVersionCode();
        }
//        return drillService.getJCSJ(url,2);
        return drillService.getJCSJ(url,3);
    }

    public RestResponse getDataTypeLevel2AndDataTypeLevel3Tree() {
        String url = basicDataUrl + "/test/dictItems/getDataTypeLevel2AndDataTypeLevel3Tree";
//        return drillService.getJCSJ(url, 2);
        return drillService.getJCSJ(url, 3);
    }
}